#ifndef __BIT_H__
#define __BIT_H__

#define BIT_SET(var, pos)                   ((var) |= (0x1<<(pos)))
#define BIT_CLR(var, pos)                   ((var) &= ~(0x1<<(pos)))
#define BIT_GET(var, pos)                   ((var) & (0x1<<(pos)))

#define BITS_MASK(bits)                     ((0x1<<((bits) + 1)) - 1)
#define BITS_MASK_GIVEN(start, end)         (BITS_MASK((end) - (start))<<(start))
#define BITS_SET(var, start, end)           ((var) |= (BITS_MASK_GIVEN((start), (end))))
#define BITS_CLR(var, start, end)           ((var) &= ~(BITS_MASK_GIVEN((start), (end))))
#define BITS_GET(var, start, end)           (((var) & BITS_MASK_GIVEN((start), (end)))>>(start))
#define BITS_SET_VAL(var, start, end, val)  do{ \
                                                BITS_CLR((var), (start), (end)); \
                                                ((var) |= (val)<<(start)); \
                                            }while(0)

#endif // __BIT_H__
